package previous.Test_0225;

/**
 * Created with Intellij IDEA
 * Description:
 * User: 星辰
 */
public class Solution {
    //字符串相乘
    //无进位相乘然后相加，最后处理进位
    public String multiply(String num1, String num2) {
        int m=num1.length();
        int n=num2.length();
        //记得翻转
        char[] c1=new StringBuilder(num1).reverse().toString().toCharArray();
        char[] c2=new StringBuilder(num2).reverse().toString().toCharArray();
        int[] tmp=new int[m+n-1];
        //无进位相加
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                //转为整数
                tmp[i+j]+=(c1[i]-'0')*(c2[j]-'0');
            }
        }
       //最后处理
        StringBuilder ret=new StringBuilder();
        int cur=0;
        int t=0;
        while(cur<m+n-1 || t!=0){
            if(cur<m+n-1)
            t+=tmp[cur++];
            ret.append(t%10);
            t/=10;
        }
        //处理前导0
        //把0删除
        while(ret.length()>1 && ret.charAt(ret.length()-1)=='0'){
            ret.deleteCharAt(ret.length()-1);
        }
        return ret.reverse().toString();
    }
    //删除字符串中所有相邻重复项
    public String removeDuplicates(String s) {
           StringBuilder stringBuilder=new StringBuilder();
           char t='0';
        for (int i = 0; i < s.length(); i++) {
            //要长度大于0才可以删，否则越界
            if(stringBuilder.length()>0 && stringBuilder.charAt(stringBuilder.length()-1)!=s.charAt(i)){
                stringBuilder.deleteCharAt(stringBuilder.length()-1);
            }else{
                stringBuilder.append(s.charAt(i));
            }
        }
        return stringBuilder.toString();
    }

}
